/*
 * File:    Test.java
 * Created: 07-Nov-2004
 * Version: $Id: //depot/bitgate/bsse/src/bitgate/util/compiler/Test.java#1 $
 *
 * COPYRIGHT (C) 2004, 2005, Bitgate Software, LLC.  All Rights Reserved.
 *
 * software@bitgatesoftware.com
 */

package com.bitgate.util.compiler;

import static com.bitgate.util.debug.Debug.*;

import java.io.File;
import java.lang.reflect.Method;

public class Test
{
    public static void main(String args[])
    {
		Compiler compiler = new Compiler("TestedCompile.java");
		StringBuffer str = new StringBuffer();
	
		compiler.setDeleteOnExit(false);
	
		str.append("import java.io.*;\n");
		str.append("import java.net.*;\n");
		str.append("import java.util.*;\n\n");
		str.append("public class TestedCompile {\n");
		str.append("    public static void main(String args[]) {\n");
		str.append("        System.err.println(\"Testing a dynamically compiled program.\");\n");
		str.append("        System.err.println(\"Test Time: '\" + System.currentTimeMillis() + \"'\");\n");
		str.append("        System.err.println(\"Build Time: '\" + new Date() + \"'\");\n");
		str.append("    }\n");
		str.append("}\n");
	
		try {
		    compiler.setCompileData(str);
		} catch(Exception e) {
		    debug("Unable to set compiler data: " + e);
		}
	
		try {
		    compiler.compile();
		} catch(CompilerException e) {
		    debug("Unable to compile: " + e.getMessage());
		}
	
		new File("TestedCompile.class").deleteOnExit();
	
		try {
		    Class<?> clazz = Class.forName("TestedCompile");
		    Method main = null;
	
		    if (clazz != null) {
			main = clazz.getMethod("main", new Class[] { String[].class });
			main.invoke(null, new Object[] { new String[0] });
		    } else {
			debug("Unable to retrieve a method 'main' for class 'TestedCompile': Class.forName failed.");
		    }
		} catch(Exception e) {
		    debug("Unable to run test object: " + e);
		}
	
		debug("You can view \"TestedCompile.java\" to test the source code.");
    }
}
